﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Control_DB_Railway
{
    /// <summary>
    /// Логика взаимодействия для Authorization.xaml
    /// </summary>
    public partial class Authorization : Window
    {
        Entities entities = new Entities();

        public Authorization()
        {
            InitializeComponent();
        }

        /* Кнопка авторизации в системе */
        private void buttonLoginSystem_Click(object sender, RoutedEventArgs e)
        {
            // Переменные для MessageBox
            string m_aut = "Аутентификация";
            string m_errorincor = "Ошибка! Проверьте правильность данных!";

            if (textBox_login.Text != "" && textBox_login.Text == null ||
                passwordBox_password.Password != "" && passwordBox_password.Password != null)
            {
                string Login = textBox_login.Text;
                string Password = passwordBox_password.Password;

                bool flag = false;

                // Просмотр записей в таблице
                foreach (var record in entities.AuthorizationTable)
                {
                    if (Login == record.login)
                    {
                        if (Password == record.password)
                        {
                            flag = true;

                            if (Login == "admin")
                            {
                                MainWindow showMainWindow = new MainWindow();

                                showMainWindow.button_AddRecord_Station.IsEnabled = true;
                                showMainWindow.button_AddRecord_TypeStation.IsEnabled = true;
                                showMainWindow.button_AddRecord_Road.IsEnabled = true;

                                showMainWindow.button_DeleteRecord_Station.IsEnabled = true;
                                showMainWindow.button_DeleteRecord_TypeStation.IsEnabled = true;
                                showMainWindow.button_DeleteRecord_Road.IsEnabled = true;

                                showMainWindow.ShowDialog();
                            }

                            else if (Login == "user")
                            {
                                MainWindow showMainWindow = new MainWindow();

                                showMainWindow.button_AddRecord_Station.IsEnabled = true;
                                showMainWindow.button_AddRecord_TypeStation.IsEnabled = true;
                                showMainWindow.button_AddRecord_Road.IsEnabled = true;

                                showMainWindow.button_DeleteRecord_Station.IsEnabled = false;
                                showMainWindow.button_DeleteRecord_TypeStation.IsEnabled = false;
                                showMainWindow.button_DeleteRecord_Road.IsEnabled = false;

                                showMainWindow.ShowDialog();
                            }
                            Close();
                            break;
                        }
                    }
                }

                if (!flag)
                    // Если пользователь не существует, то:
                    MessageBox.Show(m_errorincor, m_aut, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
                // Если данные в элементах пустые
                MessageBox.Show(m_errorincor, m_aut, MessageBoxButton.OK, MessageBoxImage.Warning);

        }
    }
}
